/*
 * PantallaMatrizCorrelacion.java
 *
 * Created on 21 de febrero de 2006, 09:47 AM
 */
package ig.metrica.ui.matrizcorrelacion;

import ejb.metrica.matriz.correlacion.MatrizDTO;
import ig.metrica.riesgo.ZonaDTO;
import ig.metrica.ui.interfaces.comunes.IMetodosPantallas;
import ig.metrica.ui.pantallas.comunes.ManipulacionDatos;
import ig.util.CodigoBotones;
import ig.util.ConexionServer;
import ig.util.NoContinuaException;
import ig.util.ejb.IGBean;
import java.awt.BorderLayout;
import java.awt.Component;
import java.sql.Date;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author  jsanchez
 */
public class PantallaEscaleraDeBandas extends javax.swing.JPanel implements IMetodosPantallas {

    private ManipulacionDatos mpd;
    private ModeloEscaleraDeBandas modelo = null;
    //private String[] columnName={};
    //private String[] columnNameFactore={"Factor","Selecci\u00F3n","Cambio Esperado"};
    //private boolean[] editFactores={true,true,true};
    private javax.swing.JFrame rootPanel = null;
    //private long fehcaInt;
    //private boolean eliminarNoConfirmar=false;
    //private Collection<ZonaDTO> CZonas=null;
    //private Collection<ig.metrica.riesgo.AjustesAlternosDTO> CAjustes=null;
    public int estado;
    private Integer id = null;
    //private Object [][]  obj=null;
    //private  int row=-1;

    /**
     * Creates new form PantallaMatrizCorrelacion
     */
    public PantallaEscaleraDeBandas(javax.swing.JFrame root) {
        this.rootPanel = root;
        IPanel pzona = new PZonas();
        IPanel pbandas = new PBandas();
        IPanel pfacotores = new PFactores();
        IPanel pfacotoresExtras = new PFactoresExtras();

        pfacotoresExtras.addOyente(pzona, 0);
        pzona.addOyente(pbandas, 0);
        pbandas.addOyente(pfacotores, 0);
        pfacotores.addOyente(pbandas, 1);
        pbandas.addOyente(pzona, 1);
        pzona.addOyente(pfacotoresExtras, 1);

        paneles.put(i++, (JPanel) pfacotoresExtras);
        paneles.put(i++, (JPanel) pzona);
        paneles.put(i++, (JPanel) pbandas);
        paneles.put(i++, (JPanel) pfacotores);


        initComponents();
        i = -1;
        panelInicial = (IPanel) paneles.get(++i);
        this.PanelEscaleraBandas.add((JPanel) panelInicial, BorderLayout.CENTER);

        mpd = new ManipulacionDatos(true, false);
        mpd.setLimpiarComponentes(true);
        mpd.setPanelMetodosPantalla(this);
        mpd.setSize(520, 400);
        mpd.setEstado(CodigoBotones.buscar + CodigoBotones.salir);
    }
    private IPanel panelInicial = null;
    Hashtable<Integer, JPanel> paneles = new Hashtable<Integer, JPanel>();

    public void addPaneles(JPanel panel) {
        paneles.put(paneles.size() + 1, panel);
    }
    Integer i = 0;

    public Integer getId() {
        return this.id;
    }

    public void setID(Integer id) {
        this.id = id;
    }

    public Date getFechaDefinicion() {
        return null;
    }

    public ManipulacionDatos getInternalFrame() {
        mpd.setResizable(false);
        mpd.setMaximizable(false);
        return mpd;
    }

    public Integer getTipo() {
        return 1;
    }

    public void validar() throws NoContinuaException {
    }

    public void guardar() throws NoContinuaException {
        try {
            modelo.setNuevo(false);
            modelo.guardar();
            Hashtable h = this.lookupMatrizCorrelacionSBBean().findById(modelo.getId());
            this.DTO = (MatrizDTO) h.get("DTO");
            panelInicial.update(h);
        } catch (Exception ex) {
            throw new NoContinuaException(ex);
        }
    }

    public void buscar() throws NoContinuaException {
        try {
            new DBuscar(modelo = new ModeloEscaleraDeBandas(this));
            if (modelo.getId() != null) {
                modelo.buscar();
                Hashtable h = this.lookupMatrizCorrelacionSBBean().findById(modelo.getId());
                this.DTO = (MatrizDTO) h.get("DTO");
                panelInicial.update(h);
            } else {
                this.mpd.setEstado(CodigoBotones.grupoEntrada - CodigoBotones.nuevo);
            }
        } catch (Exception ex) {
            throw new NoContinuaException(ex);
        }
    }
    private String ESTADO = "";
    private MatrizDTO DTO = null;

    public void modificar() throws NoContinuaException {
        //mpd.setEstado(CodigoBotones.buscar+CodigoBotones.salir);
    }

    public void nuevo() throws NoContinuaException {
    }

    public void limpiarTablas(String n) {
    }

    public void eliminar() throws NoContinuaException {
        if (JOptionPane.showConfirmDialog(this, "Se eliminara la matriz de correlaci\u00F3n, y cada uno de los factores a sociados a ella\n ¿ Desea eliminarla ? ", "Mensaje", JOptionPane.OK_CANCEL_OPTION) == 0) {
            this.modelo.eliminar();
            this.limpiarTablas("1");
        } else {
            this.mpd.setEstado(CodigoBotones.grupoEntrada + CodigoBotones.eliminar + CodigoBotones.modificar);
        }
    }

    public void salir() throws NoContinuaException {
    }

    public void imprimir() throws NoContinuaException {
    }

    public void cancelar() throws NoContinuaException {
        try {
            Hashtable h = this.lookupMatrizCorrelacionSBBean().findById(modelo.getId());
            this.DTO = (MatrizDTO) h.get("DTO");
            panelInicial.update(h);
        } catch (Exception ex) {
            throw new NoContinuaException(ex);
        }
    }

    public Component[] getControles() {
        return null;
    }

    public void setFrame(JFrame frame) {
    }

    public JFrame getFrame() {
        return this.rootPanel;
    }

    public void cerrando() throws NoContinuaException {
    }

    public String getESTADO() {
        return ESTADO;
    }

    private JFormattedTextField getFormattedTextField() {
        return null;
    }

    public void setAjustes(Collection<ig.metrica.riesgo.AjustesAlternosDTO> _c) {
    }

    public void setZonas(Collection<ZonaDTO> c) {
    }

    private synchronized void updateCollection() {
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private String tablaActual = "TZonas";
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BGTablas = new javax.swing.ButtonGroup();
        PanelEscaleraBandas = new javax.swing.JPanel();
        Sguiente = new javax.swing.JButton();
        Anterior = new javax.swing.JButton();

        PanelEscaleraBandas.setBackground(new java.awt.Color(255, 255, 255));
        PanelEscaleraBandas.setLayout(new java.awt.BorderLayout());

        Sguiente.setText("Siguiente");
        Sguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SguienteActionPerformed(evt);
            }
        });

        Anterior.setText("Anterior");
        Anterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AnteriorActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, PanelEscaleraBandas, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(Anterior, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 98, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Sguiente, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(PanelEscaleraBandas, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Sguiente)
                    .add(Anterior))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    int actual = 0;
    private void AnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnteriorActionPerformed
        anterior();
    }//GEN-LAST:event_AnteriorActionPerformed

    private void SguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SguienteActionPerformed
        siguiente();
    }//GEN-LAST:event_SguienteActionPerformed

    public void siguiente() {
        try {
            if (i < paneles.size() - 1) {
                ((IPanel) paneles.get(i)).notificarSiguiente();
                this.PanelEscaleraBandas.remove(paneles.get(i));
                this.PanelEscaleraBandas.add(paneles.get(++i), BorderLayout.CENTER);
                this.updateUI();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
    }

    public void anterior() {
        try {
            if (i >= 1 && i < paneles.size()) {
                ((IPanel) paneles.get(i)).notificarAnterior();
                this.PanelEscaleraBandas.remove(paneles.get(i));
                this.PanelEscaleraBandas.add(paneles.get(--i), BorderLayout.CENTER);
                this.updateUI();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
    }

    public boolean isUltimo() {
        if (i.equals(paneles.size())) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isPrimero() {
        if (i.equals(0)) {
            return true;
        } else {
            return false;
        }
    }
    Integer row_selecionada = -1;

    private void seleciorarCampo(String nombre) {
        tablaActual = nombre;
    }

    private synchronized void updateVistas() {
    }
    private Properties prop = null;


    {
        prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY, IGBean.INITIAL_CONTEXT_FACTORY);
        String Sockec = new ConexionServer().getSocketServer();
        prop.put(Context.PROVIDER_URL, Sockec);
    }

    private ejb.metrica.matriz.correlacion.MatrizCorrelacionSBRemote lookupMatrizCorrelacionSBBean() {
        try {
            javax.naming.Context c = new javax.naming.InitialContext(prop);
            Object remote = c.lookup("MatrizCorrelacionSBBean");
            ejb.metrica.matriz.correlacion.MatrizCorrelacionSBRemoteHome rv = (ejb.metrica.matriz.correlacion.MatrizCorrelacionSBRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, ejb.metrica.matriz.correlacion.MatrizCorrelacionSBRemoteHome.class);
            return rv.create();
        } catch (javax.naming.NamingException ne) {
            throw new RuntimeException(ne);
        } catch (javax.ejb.CreateException ce) {
            throw new RuntimeException(ce);
        } catch (java.rmi.RemoteException re) {
            throw new RuntimeException(re);
        }
    }

    public Collection getZonas() {
        return null;
    }

    public Collection getAjustesAlternos() {
        return null;
    }

    public MatrizDTO getDTO() {
        return this.DTO;
    }

    void setDataMatriz(String string, String string0, String string1, String string2) {
    }

    public Collection getFactoresExternos() {
        return panelInicial.getData();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Anterior;
    private javax.swing.ButtonGroup BGTablas;
    private javax.swing.JPanel PanelEscaleraBandas;
    private javax.swing.JButton Sguiente;
    // End of variables declaration//GEN-END:variables
}